Wrap try_init_host futures in tokio::spawn so they don't get cancelled#2857
Merged
Conversation
…elled Comment pasted from within `watch_maybe_launch_module_host`: > `try_init_host` is not cancel safe, as it will spawn other async tasks > which hold a filesystem lock past when `try_init_host` returns or is cancelled. > This means that, if `try_init_host` is cancelled, subsequent calls will fail. > > This is problematic because Axum will cancel its handler tasks if the client disconnects, > and this method is called from Axum handlers, e.g. for the subscribe route. > `tokio::spawn` a task to build the `Host` and install it in the `guard`, > so that it will run to completion even if the caller goes away. > > Note that `tokio::spawn` only cancels its tasks when the runtime shuts down, > at which point we won't be calling `try_init_host` again anyways. This fixes (hopefully) the dreaded replay-loop bug, which we belive was caused by Axum dropping the `watch_maybe_launch_module_host` future after it acquired the `Lockfile` and leaked that file to a non-cancel-able async task, leading subsequent calls to fail due to the `Lockfile` still being held. Now, any call to `watch_maybe_launch_module_host` which enters `try_init_host` will run to completion and install the new `Host` into the `HostController`, even if the calling future is dropped or cancelled.
Contributor
Author
|
@Kasama reports that manual testing in a production-like deployment no longer showed the bug in question. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Changes
Comment pasted from within
watch_maybe_launch_module_host:This fixes (hopefully) the dreaded replay-loop bug, which we belive was caused by Axum dropping the
watch_maybe_launch_module_hostfuture after it acquired theLockfileand leaked that file to a non-cancel-able async task, leading subsequent calls to fail due to theLockfilestill being held. Now, any call towatch_maybe_launch_module_hostwhich enterstry_init_hostwill run to completion and install the newHostinto theHostController, even if the calling future is dropped or cancelled.Expected complexity level and risk
1: wrapping already-async stuff in
tokio::spawnis a minimal change.Testing